package com.taxigang.client.uibinder;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasAllFocusHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.taxigang.client.Session;
import com.taxigang.client.micro.Hideable;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.ButtonType;
import com.taxigang.client.ui.map.MapView;
import com.taxigang.client.ui.map.MapView.ButtonMode;
import com.taxigang.client.ui.map.MapView.TextBoxType;
import com.taxigang.client.ui.map.SearchView;
import com.taxigang.shared.TaxiGangInfo;

public class SearchAndCreate extends TaxiGangPanel implements SearchView{

	private static SearchAndCreateUiBinder uiBinder = GWT
	.create(SearchAndCreateUiBinder.class);

	interface SearchAndCreateUiBinder extends UiBinder<Widget, SearchAndCreate> {
	}

	private Map<ButtonType,Button>searchCreateButtonMap = new HashMap<ButtonType,Button>();

	//	MultiWordSuggestOracle citySuggestOracle;  
	//	MultiWordSuggestOracle addressSuggestOracle;  

	@UiField
	SuggestBox startSuggestion;


	@UiField
	SuggestBox destinationSuggestion;

	@UiField
	Button searchButton;

	@UiField
	Button createButton;


	@UiField
	Button okButtonDepartureTime;

	@UiField
	Label startLabel;
	@UiField
	Label destinationLabel;

	@UiField
	Label headerLabel;

	@UiField
	Label departureLabel;

	@UiField
	ListBox listBoxDestinationTime;

	@UiField
	Panel searchButtonPanel;

	@UiField
	DateBox dopDateBox;

//	@UiField
//	GoogleMapsSuggestBox googleMapsSuggestBox;
	
	private int[]timeValues = new int[24*4];

	//		
	//	@UiField
	//	Panel timePanel;

	public SearchAndCreate() {
		initWidget(uiBinder.createAndBindUi(this));

		headerLabel.setText(TaxiGangCreator.taxiGangConstants.search());
		//layoutPanel.setBorderWidth(10);
		searchButton.setText(TaxiGangCreator.taxiGangConstants.searchForTaxiGang());
		createButton.setText(TaxiGangCreator.taxiGangConstants.create());

		okButtonDepartureTime.setText("OK");
		startLabel.setText(TaxiGangCreator.taxiGangConstants.enterStart());
		destinationLabel.setText(TaxiGangCreator.taxiGangConstants.enterDestination());
		departureLabel.setText(TaxiGangCreator.taxiGangConstants.departureTime());

		searchCreateButtonMap.put(ButtonType.Create, createButton);
		searchCreateButtonMap.put(ButtonType.Search, searchButton);

		Date calendar = new Date();
		//calendar.setMinutes(calendar.getMinutes()+30);
		int nowHour = calendar.getHours();
		int nowQuarter = calendar.getMinutes()/15;
		nowQuarter++;
		if (nowQuarter >=4){
			nowQuarter = 0;
			nowHour++;
		}
		int index =0;
		int nowindex =0;
		boolean begin = true;
		for (int h=0; h < 24+nowHour; h++){
			for (int q=0; q < 4; q++){
				if (begin){
					begin=false;
					q=nowQuarter;
				}
				if (h==nowHour){
					nowindex=index;
				}
				boolean today = (h<24);
				int displayHour = h;
				if (!today){
					displayHour -=24;
				}
				if (today){
					int mins =  q*15;
					int value = displayHour*60 + mins;
					String display = displayHour+":"+mins;
					if (mins==0){
						display +="0";
					}
					if (!today){
						display += " "+TaxiGangCreator.taxiGangConstants.tomorrow();
					}
					listBoxDestinationTime.addItem(display,""+value);
					timeValues[index++]=value;
				}
			}
		}
		listBoxDestinationTime.setSelectedIndex(nowindex);

		dopDateBox.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("dd.MM")));
		dopDateBox.setValue(new Date());
		dopDateBox.addValueChangeHandler(new ValueChangeHandler<Date>(){

			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				//Window.alert(""+dopDateBox.getValue().getDay() + "");
			}

		});


		setMode(SearchDisplayMode.START);

//		final Map<SuggestBox,Button>suggestBoxMap = new HashMap<SuggestBox, Button>();
//		suggestBoxMap.put(startCitySuggestion, okButtonStartCity);
//		suggestBoxMap.put(startAddressSuggestion, okButtonStartAddress);
//		suggestBoxMap.put(destinationCitySuggestion, okButtonDestinationCity);
//		suggestBoxMap.put(destinationAddressSuggestion, okButtonDestinationAddress);
//		for (final SuggestBox suggestBox : suggestBoxMap.keySet()){
//			suggestBox.getTextBox().addFocusHandler(new FocusHandler() {
//
//				@Override
//				public void onFocus(FocusEvent event) {
//					for (Button b: okButtonMap.values()){
//						b.setVisible(false);
//					}
//					suggestBoxMap.get(suggestBox).setVisible(true);
//
//				}
//			});
//		}


	}

//	public void initOracle(){
//		Session session = Session.getInstance();
//		if (session != null && session.getTaxiGangUserInfo() != null){
//
//			//MultiWordSuggestOracle startCityOracle = (MultiWordSuggestOracle)startCitySuggestion.getSuggestOracle();  
//			MultiWordSuggestOracle startAddressOracle = (MultiWordSuggestOracle)startAddressSuggestion.getSuggestOracle();  
//			MultiWordSuggestOracle destCityOracle = (MultiWordSuggestOracle)destinationCitySuggestion.getSuggestOracle();  
//			MultiWordSuggestOracle destAddressOracle = (MultiWordSuggestOracle)destinationAddressSuggestion.getSuggestOracle();  
//
//			String[] cityList = session.getTaxiGangUserInfo().getCityList();
//			for (String suggestion : cityList){
//				//startCityOracle.add(suggestion);	
//				destCityOracle.add(suggestion);	
//			}
//			if (cityList.length > 0){
//
//				session.getSearchTaxiGangInfo().setStartCity(cityList[0]);
//				session.getSearchTaxiGangInfo().setDestinationCity(cityList[0]);
//				//				startCitySuggestion.setText(cityList[0]);
//				//				destinationCitySuggestion.setText(cityList[0]);
//				//setMode(SearchDisplayMode.START_ADDRESS);
//				//				if (presenter != null ){
//				//					presenter.showStartAddress(cityList[0]);
//				//				}
//			}
//			String[] addressList = session.getTaxiGangUserInfo().getAddressList();
//			for (String suggestion : addressList){
//				startAddressOracle.add(suggestion);	
//				destAddressOracle.add(suggestion);	
//			}
//		}
//
//	}

	@Override
	public Hideable getHideable() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HasText getTextBoxText(MapView.TextBoxType tbt){
		HasText tb = null;
		switch (tbt){
		case START: 
			tb = startSuggestion.getTextBox();  
			break;
		case DESTINATION: tb = destinationSuggestion.getTextBox();  break;

		}
		return tb;
	}

	@Override
	public HasKeyPressHandlers getTextBox(TextBoxType tbt) {
		HasKeyPressHandlers tb = null;
		switch (tbt){
		case START: tb = startSuggestion.getTextBox();  break;
		case DESTINATION: tb = destinationSuggestion.getTextBox();  break;

		}
		return tb;
	}

	@Override
	public SuggestBox getSuggestBox(TextBoxType tbt) {
		SuggestBox tb = null;
		switch (tbt){
		case START: 
			tb = startSuggestion;  
			break;
		case DESTINATION: tb = destinationSuggestion;  break;

		}
		return tb;
	}

	
	@Override
	public HasSelectionHandlers<Suggestion> getSuggestion(TextBoxType tbt) {
		HasSelectionHandlers<Suggestion> tb = null;
		switch (tbt){
		case START: tb = startSuggestion;  break;
		case DESTINATION: tb = destinationSuggestion;  break;

		}
		return tb;
	}

	@Override
	public HasClickHandlers getButton(ButtonType buttonType) {
		return searchCreateButtonMap.get(buttonType);
	}

	@Override
	public HasAllFocusHandlers getFocus(TextBoxType tbt) {
		HasAllFocusHandlers tb = null;
		switch (tbt){
		case START: tb = startSuggestion.getTextBox();  break;
		case DESTINATION: tb = destinationSuggestion.getTextBox();  break;

		}
		return tb;
	}



	@Override
	public HasClickHandlers getOKTimeButton() {
		return okButtonDepartureTime;
	}

	@Override
	public void updateDepartureTime() {
		int index = listBoxDestinationTime.getSelectedIndex();
		int value = timeValues[index];
		int hour = value/60;
		int min = value%60;
		TaxiGangInfo info = Session.getInstance().getSearchTaxiGangInfo();

		info.setDepartureHour(hour);
		info.setDepartureMinute(min);
		Date val = dopDateBox.getValue();
		info.setDepartureDate(val.getDate(),val.getMonth(),val.getYear());
		info.setCreationTime(new Date());

	}

	@Override
	public void setMode(SearchDisplayMode mode) {

		switch (mode){
		case START:
			//startAddressSuggestion.setFocus(true);
			searchButtonPanel.setVisible(false);
			break;
		case DESTINATION:
			destinationLabel.setVisible(true);
			destinationSuggestion.setVisible(true);
			destinationSuggestion.setFocus(true);
			break;
		case TIME:
			departureLabel.setVisible(true);
			listBoxDestinationTime.setVisible(true);
			dopDateBox.setVisible(true);
			okButtonDepartureTime.setVisible(true);
			break;
		case SEARCH_CREATE:
			destinationLabel.setVisible(true);
			destinationSuggestion.setVisible(true);
			
			departureLabel.setVisible(true);
			listBoxDestinationTime.setVisible(true);
			dopDateBox.setVisible(true);
			searchButtonPanel.setVisible(true);
			okButtonDepartureTime.setVisible(false);
			//						searchButton.setVisible(true);
			//						searchButton.setFocus(true);
			break;



		}

	}


	@Override
	public void setStart(String start) {
		startSuggestion.getTextBox().setText(start);
	}

	@Override
	public void setDestination(String destination) {
		destinationSuggestion.getTextBox().setText(destination);

	}

	@Override
	public void setTextBoxText(TextBoxType tbt, String text) {
		Window.alert("TODO:setTextBoxText");

	}

	@Override
	public void setButtonMode(ButtonMode mode) {
		Window.alert("TODO:setButtonMode");

	}

	public String getSelectedTime() {
		// TODO Auto-generated method stub
		return null;
	}


}
